package daoImpl;

import dao.UserDao;
import entity.User;
import baseDao.BaseDao;

import java.util.List;

/**
 * Created by 2015302580057 on 2017/7/11.
 */
public class UserDaoImpl extends BaseDao implements UserDao {


    public int addUser(User u) {
        Object [] obs={u.getUsername(),u.getPassword(),u.getEmail()};
        return executeUpdate("INSERT INTO user(userName, password, email) VALUES (?,?,?)", obs );
    }

    public int deleteUser(String uname) {
        return 0;
    }


    public int updateUser(User u) {
        Object[] objs = {u.getSex(),u.getProfession(),u.getDescribe(),u.getUsername()};
        return executeUpdate("UPDATE user SET sex=?,profession=?,`describe`=? WHERE username = ?",objs);
    }

    public int updateUserPassword(User u){
        Object[] objs = {u.getPassword(),u.getEmail()};
        return executeUpdate("UPDATE user SET password = ? WHERE email = ?",objs);
    }

    public User queryUserByEmail(String email) {
        List<User> list=executeQuery("SELECT * FROM USER WHERE EMAIL ='"+email+"'", null, User.class);
        if(list.size()>0)
            return list.get(0);
        return null;
    }
    public User queryUserByUserName(String name) {
        List<User> list=executeQuery("SELECT * FROM USER WHERE username ='"+name+"'", null, User.class);
        if(list.size()>0)
            return list.get(0);
        return null;
    }

    public List<User> queryFollowToByUserName(String name){
        List<User> list=executeQuery("SELECT * FROM user WHERE username IN (SELECT `to` FROM follow WHERE `from` = '"+name+"')", null, User.class);
        return list;
    }


    public User login(User u) {
        return null;
    }

    public List queryAllUser() {
        return null;
    }
}
